package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    /**
     * 添加分类数据
     *
     * @param category
     * @return
     */
    @Override
    public boolean save(Category category) {
        //1.完善数据
        //创建时间
        category.setCreateTime(LocalDateTime.now());
        //修改时间
        category.setUpdateTime(LocalDateTime.now());

        //2.判断是否存在
        Category dbCategory = categoryMapper.findByName(category.getName());
        if (dbCategory != null){
            //不为null,已经存在类别
            throw new BusinessException("分类名称已经存在,请修改!");
        }

        //3.返回数据
        return categoryMapper.save(category)>0;
    }

    /**
     * 分页获取数据
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Category> categoryList = categoryMapper.getAll();
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

        Page<Category> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());
        return pageObj;
    }

    /**
     * 根据id删除类别
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Long id) {
        //1.获取类别菜品数量,如果大于0,提示用户"该类别有菜品无法删除"
        Integer dishCount = dishMapper.CountByCid(id);
        if (dishCount>0){
            throw new BusinessException("该类别有菜品无法删除");
        }
        //2.获取类别套餐数量,如果大于0,提示用户"该类别有套餐无法删除"
        Integer setmealCount = setmealMapper.CountByCid(id);
        if (setmealCount>0){
            throw new BusinessException("该类别有菜品无法删除");
        }
        //3.如果上面方法都跳过,调用数据访问层删除
        return categoryMapper.deleteById(id)>0;
    }

    /**
     * 类别修改
     *
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {
        //获取数据
        Category categoryObj = categoryMapper.getByName(category);

        //1.判断这个类别是否存在
        if (categoryObj !=null && !categoryObj.getId().equals(category.getId())){
            throw new BusinessException("该类别已存在,请修改!");
        }
        //2.完善信息(修改时间)
        category.setUpdateTime(LocalDateTime.now());

        //3.操作修改
        return categoryMapper.update(category)>0;
    }

    @Override
    /**
     * 查询所有分类数据
     * @return List
     */
    public List<Category> getAll() {
        return categoryMapper.getAll();
    }

    /**
     * 根据type查询分类列表
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }
}
